package com.example.saiweisi.service.impl.userService;

import com.example.saiweisi.controller.request.DiancanRequest;
import com.example.saiweisi.mapper.userMapper.OrderCaipinMapper;
import com.example.saiweisi.mapper.userMapper.OrderMapper;
import com.example.saiweisi.model.Order;
import com.example.saiweisi.model.OrderCaipin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DiancanService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderCaipinMapper orderCaipinMapper;

    public void submit(Integer userId, Double sum ,List<DiancanRequest> diancanRequest) {
        Order order = new Order();
        order.setSubmitTime(new Date());
        order.setUserId(userId);
        order.setOrderPrice(sum);
        orderMapper.insertSelective(order);

        for (DiancanRequest temp:diancanRequest) {
            OrderCaipin orderCaipin = new OrderCaipin();
            orderCaipin.setOrderId(order.getOrderId());
            orderCaipin.setCaipinId(Integer.valueOf(temp.getId()));
            orderCaipin.setCount(temp.getCount());
            orderCaipinMapper.insertSelective(orderCaipin);
        }
    }

    public void update(Integer orderId, Double sum ,List<DiancanRequest> diancanRequest) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderPrice(sum);
        orderMapper.updateByPrimaryKeySelective(order);
        orderCaipinMapper.deleteByOrderId(orderId);
        for (DiancanRequest temp:diancanRequest) {
            OrderCaipin orderCaipin = new OrderCaipin();
            orderCaipin.setOrderId(orderId);
            orderCaipin.setCaipinId(Integer.valueOf(temp.getId()));
            orderCaipin.setCount(temp.getCount());
            orderCaipinMapper.insertSelective(orderCaipin);
        }
    }
}
